using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SystemFunctionLib.SystemEnvironment;

namespace SystemFunctionDemo
{
    public partial class SystemEnvForm : Form
    {
        public SystemEnvForm()
        {
            InitializeComponent();
            this.Font = SystemFonts.DialogFont;

            LoadSettings();
        }

        private void LoadSettings()
        {
            LoadCurrentDateTime();
            timerDateTime.Start();
        }

        private bool _dateModified = false;
        private bool _timeModified = false;
        private void LoadCurrentDateTime()
        {
            if (!_dateModified)
            {
                pickerLocalDate.Value = SystemDateTime.LocalDateTime;
                _dateModified = false;
            }

            if (!_timeModified)
            {
                pickerLocalTime.Value = SystemDateTime.LocalDateTime;
                _timeModified = false;
            }
            else
            {
                timerDateTime.Stop();
            }
        }

        private void SaveSettings()
        {
            DateTime date = pickerLocalDate.Value.Date;
            TimeSpan time = pickerLocalTime.Value.TimeOfDay;

            SystemDateTime.LocalDateTime = date.Add(time);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            SaveSettings();
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            SaveSettings();
        }

        private void timerDateTime_Tick(object sender, EventArgs e)
        {
            timerDateTime.Interval = 1000;
            LoadCurrentDateTime();
        }

        private void pickerLocalDate_ValueChanged(object sender, EventArgs e)
        {
            _dateModified = true;
        }

        private void pickerLocalTime_ValueChanged(object sender, EventArgs e)
        {
            _timeModified = true;
        }
    }
}